import request from '@/utils/request';
import { requestWrapper, randomDate, randomInt, randomPick, paginate } from '@/utils/mock';

// 生成固定的部门-岗位关联数据，使模拟数据更加一致
const getDeptPostMapping = () => {
  // 固定的部门集合
  const departments = [
    { deptId: 1, deptName: '总公司' },
    { deptId: 11, deptName: '研发部门', parentId: 1 },
    { deptId: 12, deptName: '市场部门', parentId: 1 },
    { deptId: 2, deptName: '分公司' },
    { deptId: 21, deptName: '技术部门', parentId: 2 },
    { deptId: 22, deptName: '运营部门', parentId: 2 }
  ];
  
  // 将岗位分配到各个部门
  return departments;
};

// 获取岗位列表
export function getPostList(params) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/post/list',
      method: 'get',
      params
    }),
    // 模拟数据
    () => {
      const statusOptions = ['0', '1'];
      const departments = getDeptPostMapping();
      
      // 添加部门信息到岗位数据中
      const posts = [];
      
      // 为每个部门生成岗位
      departments.forEach(dept => {
        // 每个部门生成1-3个岗位
        const deptPosts = Array.from({ length: randomInt(1, 3) }, (_, index) => {
          const postId = posts.length + 1;
          return {
            postId,
            postName: `${dept.deptName}岗位${index + 1}`,
            postCode: `DEPT${dept.deptId}_POST${index + 1}`,
            postSort: postId,
            status: randomPick(statusOptions),
            deptId: dept.deptId,
            deptName: dept.deptName,
            createTime: randomDate(-30)
          };
        });
        
        posts.push(...deptPosts);
      });

      // 处理搜索条件
      let filteredPosts = [...posts];

      if (params) {
        if (params.postName) {
          filteredPosts = filteredPosts.filter(item => 
            item.postName.includes(params.postName)
          );
        }
        
        if (params.postCode) {
          filteredPosts = filteredPosts.filter(item => 
            item.postCode.includes(params.postCode)
          );
        }
        
        if (params.status !== undefined && params.status !== '') {
          filteredPosts = filteredPosts.filter(item => 
            item.status === params.status
          );
        }
        
        // 添加部门ID过滤条件，确保类型一致进行比较
        if (params.deptId !== undefined && params.deptId !== '') {
          const deptIdNum = parseInt(params.deptId);
          filteredPosts = filteredPosts.filter(item => {
            // 直接匹配部门ID
            if (item.deptId === deptIdNum) {
              return true;
            }
            
            // 查找所有部门关系，支持按父部门筛选
            const dept = departments.find(d => d.deptId === item.deptId);
            if (dept && dept.parentId === deptIdNum) {
              return true;
            }
            
            return false;
          });
        }
      }

      // 处理分页
      const pageNum = params?.pageNum ? parseInt(params.pageNum) : 1;
      const pageSize = params?.pageSize ? parseInt(params.pageSize) : 10;
      const startIndex = (pageNum - 1) * pageSize;
      const endIndex = pageNum * pageSize;
      const pageData = filteredPosts.slice(startIndex, endIndex);

      return paginate(pageData, pageNum, pageSize, filteredPosts.length);
    }
  );
}

// 获取岗位详情
export function getPostDetail(postId) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/post/${postId}`,
      method: 'get'
    }),
    // 模拟数据
    () => {
      const departments = getDeptPostMapping();
      const postIdNum = parseInt(postId);
      
      // 根据postId找到合适的部门
      const deptIndex = (postIdNum % departments.length);
      const dept = departments[deptIndex];
      
      return {
        postId: postIdNum,
        postName: `${dept.deptName}岗位${postIdNum % 3 + 1}`,
        postCode: `DEPT${dept.deptId}_POST${postIdNum % 3 + 1}`,
        postSort: postIdNum,
        status: '1',
        deptId: dept.deptId,
        deptName: dept.deptName,
        remark: `${dept.deptName}岗位${postIdNum % 3 + 1}的描述信息`,
        createTime: randomDate(-30),
        updateTime: randomDate(-10)
      };
    }
  );
}

// 新增岗位
export function addPost(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/post',
      method: 'post',
      data
    }),
    // 模拟数据
    {
      postId: randomInt(100, 999)
    }
  );
}

// 更新岗位
export function updatePost(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/post',
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 删除岗位
export function deletePost(postIds) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/post/${postIds}`,
      method: 'delete'
    }),
    // 模拟数据
    null
  );
}

// 导出岗位数据
export function exportPost(params) {
  return request({
    url: '/system/post/export',
    method: 'get',
    params,
    responseType: 'blob'
  });
}

// 更新岗位状态
export function updatePostStatus(data) {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: `/system/post/status`,
      method: 'put',
      data
    }),
    // 模拟数据
    null
  );
}

// 获取岗位选项列表
export function getPostOptions() {
  return requestWrapper(
    // 真实API请求
    () => request({
      url: '/system/post/options',
      method: 'get'
    }),
    // 模拟数据
    Array.from({ length: 5 }, (_, index) => ({
      postId: index + 1,
      label: `岗位${index + 1}`
    }))
  );
} 